home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
3_0
/
OIC_1
/
OIC_SOUR
/
COLLECT.C
< prev
next >
Wrap
Text File
|
1989-03-05
|
3KB
|
134 lines
/*
* Collection abstract class
*
* Andrew Nicholson - Not-So-Soft, Oz.
*
* Provides the following methods to any subclass that responds to
* head, tail, push, isEmpty
*
* append c item - appends item to collection c
* copy c - returns a new collection which is a copy of c
* add c itemlist - adds items to collection
* sequence c - returns an initialised sequence object containing c
* repList c - returns a representation list for c (ascii).
* print c - outputs nicely formatted collection c to stdout
* deepDispose c - dispose c and sends 'deepDispose' to all it contains
* dispose c - free the collection leaving what it contains
* deepCopy c - returns a deep copy of c
*/
#include "oic.h"
#include "generics.h"
class Collect;
/* -------------------- Collection methods ---------------------------- */
method object
_append(self, inst, item) /* append item to the collection */
object self;
void *inst;
object *item;
{
register object c;
for (c = self ; ! (int)isEmpty(c) ; c = tail(c));
push(c, *item);
return self;
}
method object
_copy(self, inst, args) /* Copy the collection */
object self;
void *inst, *args;
{
if ((int)isEmpty(self))
return New(ClassOf(self));
else
return push(copy(tail(self)), head(self));
}
method object
_add(self, inst, items) /* add items to collection */
object self;
void *inst;
register object *items;
{
while (*items != END)
append(self, *items++);
return self;
}
method object
_sequence(self) /* get a sequence over collection */
object self;
{
return start(New(Linkseq), self);
}
method object
_repList(self) /* get a represention list */
object self;
{
register object c;
register object replist;
replist = New(Replist, "(", ")", " ");
if ((int)isEmpty(self))
append(replist, New(String, "EMPTY"));
else for (c = self; ! (int)isEmpty(c); c = tail(c))
append(replist, repList(head(c)));
return replist;
}
method void
_deepDispose(self)
object self;
{
register object c, nextc;
register object item;
for (c = self; !(int)isEmpty(c); c = nextc)
{
nextc = tail(c);
deepDispose(head(c));
free(c);
}
free(c);
}
method void
_dispose(self)
object self;
{
register object c, nextc;
for (c = self; ! (int)isEmpty(c) ; c = nextc)
{
nextc = tail(c);
free(c);
}
free(c);
}
/* ------------------- Init the Collection class ---------------------- */
InitCollect()
{
Collect = NewClass(0, 0, "Collect", END);
AddMethods(Collect,
appendGeneric, _append,
copyGeneric, _copy,
addGeneric, _add,
sequenceGeneric, _sequence,
repListGeneric, _repList,
deepDisposeGeneric, _deepDispose,
disposeGeneric, _dispose,
END
);
}